/*
 * @# ShardRedis.java 2011-11-10 下午05:33:08
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.experiments.redis.shard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.pool.impl.GenericObjectPool.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

/**
 * @author alvin hwang
 */
public class ShardRedis {
	private static Logger log = LoggerFactory.getLogger(ShardRedis.class);
	
	private ShardedJedis jedis;

	public ShardRedis() {
		JedisShardInfo shard1 = new JedisShardInfo("192.168.1.104", 6379, "redis-104");
		JedisShardInfo shard2 = new JedisShardInfo("192.168.1.105", 6379, "redis-105");
		
		List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
		shards.add(shard1);
		shards.add(shard2);

		Config config = new Config();
		ShardedJedisPool pool = new ShardedJedisPool(config, shards);
		
		jedis = pool.getResource();
	}
	
	public void put(int count) {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		
		for (int i = 0; i < count; i++) {
			Map<String, String> cache = new HashMap<String, String>();
			cache.put("name", "value-" + i);
			
			jedis.hmset("shard-demo-" + i, cache);
		}
		
		stopWatch.stop();
		log.debug("ShardRedis put 个数: {} 总运行时间为: {}", count, stopWatch.toString());
	}
	
	public static void main(String[] args) {
		ShardRedis shardRedis = new ShardRedis();
		shardRedis.put(10);
	}
}
